package com.quwan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.app.vo.dispatch.ApplyCountDTO;
import com.quwan.app.vo.dispatch.ApplyMemberDTO;
import com.quwan.app.vo.dispatch.ServiceDispatchOrderDTO;
import com.quwan.entity.TDispatchOrderApply;
import com.quwan.manager.controller.dto.DispatchApplyDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
*
*
* @author quan
* @date 2022-05-26 11:29
*/
 

public interface TDispatchOrderApplyMapper extends BaseMapper<TDispatchOrderApply> {
    @Select("select order_id, count(id) count from t_dispatch_order_apply " +
            " where  order_id in (${ids}) and status =0 and type = 0 group by order_id")
    List<ApplyCountDTO> selectCountByIds(String ids);

    @Select(" select a.order_id,a.member_img,a.apply_m_id from t_dispatch_order_apply a where a.order_id in (${ids}) and a.status=0  " +
            " order by  a.add_time desc limit 5 ")
    List<TDispatchOrderApply> selectApplyMemberImg(String ids);


    @Select("select m.nick_name nickname,m.img_url memberImg," +
            " a.apply_m_id mId, a.apply_time applyTime,a.id applyId,a.order_id dispatchOrderId" +
            " from t_dispatch_order_apply a left  join t_member m " +
            " on a.apply_m_id = m.id where a.order_id =#{id} and type= 0 and a.status =0 order by a.add_time desc")
    Page<ApplyMemberDTO> selectPageByOrderId(Page<Object> objectPage,@Param("id") String id);

//    @Select("select " +
//            "m.id memberId,m.nick_name nickname,m.img_url memberImg," +
//            "a.add_time date, " +
//            "o.content,o.price,o.category,o.unit,o.city_name area, o.id " +
//            "from t_dispatch_order o " +
//            "left join t_member m  on o.m_id = m.id  " +
//            "left join t_dispatch_order_apply a on o.id =a.order_id " +
//            "where a.apply_m_id=#{mId} and o.status=#{type}  and a.status > 0 and a.type =0  order by a.apply_time" )
    @Select("select " +
            "m.id memberId,m.nick_name nickname,m.img_url memberImg, " +
            "a.add_time date, a.status ," +
            "o.content,o.price,o.category,o.unit,o.city_name area, o.id, o.imgs " +
            "from t_dispatch_order_apply a " +
            "left join t_dispatch_order o on o.id =a.order_id " +
            "left join t_member m  on o.m_id = m.id " +
            "where  a.apply_m_id=#{mId} and a.type = 0 and a.status= ifnull(#{type},true) order by a.apply_time ")
    Page<ServiceDispatchOrderDTO> selectApplyOrderInfo(Page page, @Param("mId") String mId, @Param("type") Integer type);

    @Insert("insert into t_dispatch_order_apply(apply_m_id,order_id,apply_time,un_lock,member_img,type,status)" +
            "values (#{applyMId} ,#{orderId} ,#{applyTime} ,#{unLock},#{memberImg},#{type} ,#{status})" +
            "ON duplicate key update status=#{status} ,apply_time=#{applyTime},member_img=#{memberImg},add_time=#{addTime}  ")
    Integer saveApplyInfo(TDispatchOrderApply tDispatchOrderApply);

    @Select("select " +
            " a.id,a.apply_time,a.add_time,a.status,m.name,m.auto_id sAuto,m.img_url img  from t_dispatch_order_apply a left  join t_member m on a.apply_m_id = m.id where  a.order_id = #{id} ")
    Page<DispatchApplyDto> selectApplyListPage(Page<Object> objectPage, @Param("id") String id);
}